<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增盘点仓库')" />
    <th:block th:include="include :: bootstrap-editable-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-checkStore-add">
            <div class="form-group hidden">
                <label class="col-sm-3 control-label">仓库：</label>
                <div class="col-sm-8">
                    <input name="warehouseId" class="form-control" type="text" th:value="1">
<!--                    <select name="warehouseId" class="form-control m-b" th:with="wareHouseList=${@sysDict.getWarehouseAll(null)}">-->
<!--                        <option value="">所有</option>-->
<!--                        <option th:each="dict : ${wareHouseList}" th:text="${dict.warehouseName}" th:value="${dict.warehouseId}"></option>-->
<!--                    </select>-->
                </div>
            </div>
            <h4 class="form-header h4">盘点产品列表</h4>
            <div class="form-group">
                <div class=" row">
                    <div class="btn-group-sm" id="toolbar" role="group">
                        <a class="btn btn-success" onclick="chooseStoreProduct()" >
                            <i class="fa fa-plus"></i> 添加
                        </a>
                        <a class="btn btn-danger multiple disabled" onclick="removeStoreProduct()">
                            <i class="fa fa-remove"></i> 删除
                        </a>
                    </div>
                    <div class="col-sm-12 select-table table-striped">
                        <table id="bootstrap-table-store"></table>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-table-editable-js" />
    <script th:inline="javascript">
        var prefix = ctx + "checkStore/checkStore"
        $("#form-checkStore-add").validate({
            focusCleanup: true
        });



        let datas=[];
        let tableId="bootstrap-table-store"
        let options = {
            id:tableId,
            data: datas,
            onEditableSave: onEditableSave,
            showSearch: false,
            showRefresh: false,
            showColumns: false,
            showToggle: false,
            columns: [{ checkbox: true },
                {
                    field: 'warehouseStoreId',
                    title: 'ID',
                    visible: false
                },
                {
                    field: 'warehouseId',
                    title: '仓库ID',
                    visible: false
                },
                {
                    field: 'warehouseName',
                    title: '仓库名称',
                    visible: false
                },
                {
                    field: 'productId',
                    title: '产品ID',
                    visible: false
                },
                {
                    field: 'name',
                    title: '产品名称'
                },
                {
                    field: 'code',
                    title: '产品编码',
                },
                {
                    field: 'unit',
                    title: '产品单位'
                },
                {
                    field: 'remainNum',
                    title: '库存剩余数量'
                },
                {
                    field: 'checkNum',
                    title: '盘点数量',
                    editable : {
                        type : 'text',
                        title : '盘点数量',
                        validate : function(value) {
                            if (value.length == 0) {
                                return '盘点数量不能为空';
                            }
                        }
                    }
                },
                {
                    field: 'remark',
                    title: '备注',
                }]
        };


        $(function() {
            $.table.init(options);
        });
        function submitHandler() {
            if ($.validate.form()) {
                var rows = $("#" +tableId).bootstrapTable('getData');
                if(isEmptyString(rows)||rows.length==0){
                    $.modal.alertWarning("请至少选择一条记录");
                    return;
                }
                let datas=$('#form-checkStore-add').serialize();
                datas=datas+"&params[list]="+JSON.stringify(rows);
                $.operate.save(prefix + "/add", datas);
            }
        }


        function chooseStoreProduct(){
            var warehouseId = $("input[name='warehouseId']").val();
            if(warehouseId == null || warehouseId == ""){
                $.modal.alertWarning("请选择仓库");
                return;
            }
            var url = ctx + "warehouse/warehouseStore?warehouseId="+warehouseId;
            $.modal.open("选择产品",url,1200,($(window).height() - 50),function (index, layero) {
                var rows = layero.find('iframe')[0].contentWindow.getStoreProductSelections();
                if (rows.length == 0) {
                    $.modal.alertWarning("请至少选择一条记录");
                    return;
                } else {
                    handStoreRowsValue(rows);
                    $.modal.close(index);
                }
            });
        }

        function handStoreRowsValue(rows){
            // var arry= mergeAndDistinct(datas.rows);
            for(var i=0;i<rows.length;i++){
                var row={};
                row.warehouseStoreId=rows[i].warehouseStoreId;
                row.warehouseId=rows[i].warehouseId;
                row.warehouseName=rows[i].warehouseName;
                row.productId=rows[i].productId;
                row.name=rows[i].name;
                row.code=rows[i].code;
                row.unit=rows[i].unit;
                row.remainNum=rows[i].remainNum||0;
                row.checkNum=rows[i].checkNum||0;
                datas.push(row)
            }
            options.data=datas;
            $.table.destroy(tableId);
            $.table.init(options);
        }

        function removeStoreProduct(){
            var ids = $.table.selectColumns("warehouseStoreId");
            if (ids.length == 0) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }
            $("#"+tableId).bootstrapTable('remove', {
                field: 'warehouseStoreId',
                values: ids
            })
        }

        function onEditableSave (field, row, rowIndex, oldValue, $el) {
            //alert("字段名：" + field + "，当前值：" + row[field]  + "，旧值：" + oldValue);
        }
    </script>
</body>
</html>
